var express = require('express');
var router = express.Router();
var User = require('../../modules/system/user');
var MD5 = require('../../utils/secure/MD5');

/* GET home page. */
router.get('/', function(req, res) {
    res.render('login');
});

router.get('/check', function(req, res) {

    router.check(req,res);
});

router.get('/loginUser', function(req, res) {
    res.json(req.session.user);
});

router.check = function (req, res) {
    var condition = {
        username: req.query.username,
        password: MD5.encrypt(req.query.password),
        status : 1
    };

    router.checking(condition,req,res)
};

router.checking = function(condition,req,res) {
    User.findOne(condition, function (err, user) {
        if (err) return handleError(err);
        if (!user) {
            res.contentType('json');
            res.json({success: false, message: '用户名或密码不正确，请重新登录'});
        }else{
            req.session.user = user;
            res.redirect('main');
        }

        console.log(err)
    })
};

module.exports = router;
